//
//  HMHomeRetWeetView.swift
//  WeiBoXR
//
//  Created by acer on 16/9/24.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit
import YYText
class HMHomeRetWeetView: UIView {

    var retweetViewBottomConstraint: Constraint?

    var statusViewModel: HMHomeCellViewModel? {
        didSet {
//            contentLabel.text = statusViewModel?.retweetContent
            //  设置转发微博的富文本
            contentLabel.attributedText = statusViewModel?.retweetAtrributedString

            retweetViewBottomConstraint?.uninstall()
            if let picurls: [HMStatusPictureInfo] = statusViewModel?.statusModel?.retweeted_status?.pic_urls, picurls.count > 0 {
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTVCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picurls

            } else {
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HMHomeTVCellMargin).constraint
                })
            }
        }
    }

    //  MARK: --    懒加载
    private lazy var contentLabel: YYLabel = {
//        let label = UILabel(textColor: UIColor.lightGray, fontSize: 13)
        let label = YYLabel()
        //  设置最大换行的宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HMHomeTVCellMargin
        //  多行显示
        label.numberOfLines = 0
        
        label.text = "哈哈哈 , 新浪微博, 我来了~"
        return label
        
    }()
    
    private lazy var pictureView: HMPictureStatusView = {
        let view = HMPictureStatusView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(HMHomeTVCellMargin)
            make.leading.equalTo(self).offset(HMHomeTVCellMargin)
            make.width.equalTo(ScreenWidth - 2 * HMHomeTVCellMargin)
        }
        pictureView.snp_makeConstraints(closure: { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMHomeTVCellMargin)


        })
        //  关键约束 -> 指定当前视图的底部约束=转发微博内容底部的约束+间距
        self.snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTVCellMargin).constraint
        }
        //  设置点击高亮效果执行的闭包
        
        //        @param containerView 能够显示富文本的控件
        //        @param text    ->NSAttributedString 能够显示富文本
        //        @param range    -> 匹配高亮效果的范围
        //        @param rect     -> 点击高亮效果的区域
        contentLabel.highlightTapAction = { (containerView, attributedString, range, rect) in
            //  根据匹配的范围获取对应的字符串
            let text = (attributedString.string as NSString).substring(with: range)
            /// ......判断数据是什么类型, 去做相应的处理
            if text.hasPrefix("http") {
                print("你点的是链接")
            }
            print(text)
        }

    }


}
